package com.ruoyi.api.activiti.listener;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.projectInfo.domain.ProjectInfo;
import com.ruoyi.project.projectInfo.service.IProjectInfoService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("projectLeaderListener")
@Slf4j
public class ProjectLeaderListener implements TaskListener {
  @Autowired
  private IProjectInfoService projectInfoService;

  @Autowired
  private ISysUserService userService;
  @Override
  public void notify(DelegateTask delegateTask) {
    System.out.println(delegateTask);
    String eventName = delegateTask.getEventName();
    if (eventName.equalsIgnoreCase(TaskListener.EVENTNAME_CREATE)) {
      Map<String, Object> variables = delegateTask.getVariables();
      if (variables.containsKey("projectId") && variables.containsKey("service")) {
        Object projectId = variables.get("projectId");
        Object service = variables.get("service");
        log.error("===ProjectLeaderListener=="+String.valueOf(projectId));
        if (projectId instanceof String && service instanceof String) {
          String v1 = (String) projectId;
          ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(Long.parseLong(v1));
          Long leaderId = projectInfo.getLeaderId();
          SysUser sysUser = userService.selectUserById(leaderId);

          if(sysUser!=null && !StringUtils.isEmpty(sysUser.getLoginName()))
          {
            log.error("===项目经理分配给了=="+sysUser.getLoginName()+"--"+sysUser.getUserName());
            delegateTask.setAssignee(sysUser.getLoginName());
          }

        }
      }
    }
  }


}
